<?php


namespace app\admin\service;


use app\admin\model\ShortLink;
use app\common\validate\CommonValidate;
use think\exception\ValidateException;
use think\facade\Request;

class LinkService
{
    public static function goAdd()
    {
        if (Request::isPost()) {
            $data = Request::post();
            //验证
            $rule = [
                'link_code' => 'require',
                'link_url' => 'require|url',
                'expire_time' => 'require',
            ];
            $message = [
                'link_code.require' => "短链接code不能为空",
                'link_url.require' => "长链接地址不能为空",
                'expire_time.require' => "过期时间不能为空",
            ];
            $check = Request::checkToken('__token__', $data);
            if (false === $check) {
                throw new ValidateException('invalid token');
            }
            if (($msg = CommonValidate::check($rule, $message, $data)) !== true)
                return ['msg' => $msg, 'code' => 201];
            unset($data['__token__']);
            $data['expire_time'] = strtotime($data['expire_time']);
            try {
                $exist = ShortLink::checkExist(0, $data['link_code']);
                if ($exist) return ['msg' => '短链接code已存在', 'code' => 201];
                ShortLink::create($data);
                return ['msg' => '操作成功', 'code' => 200];
            } catch (\Exception $e) {
                return ['msg' => '操作失败' . $e->getMessage(), 'code' => 201];
            }
        }
    }

    public static function goEdit()
    {
        if (Request::isPost()) {
            $data = Request::post();
            //验证
            $rule = [
                'link_id' => 'require',
                'link_code' => 'require',
                'link_url' => 'require|url',
                'expire_time' => 'require',
            ];
            $message = [
                'link_id.require' => "短链接id不能为空",
                'link_code.require' => "短链接code不能为空",
                'link_url.require' => "长链接地址不能为空",
                'expire_time.require' => "过期时间不能为空",
            ];
            $check = Request::checkToken('__token__', $data);
            if (false === $check) {
                throw new ValidateException('invalid token');
            }
            if (($msg = CommonValidate::check($rule, $message, $data)) !== true)
                return ['msg' => $msg, 'code' => 201];
            unset($data['__token__']);
            $data['expire_time'] = strtotime($data['expire_time']);
            try {
                $exist = ShortLink::checkExist($data['link_id'], $data['link_code']);
                if ($exist) return ['msg' => '来源名称已存在', 'code' => 201];
                ShortLink::update($data);
                return ['msg' => '操作成功', 'code' => 200];
            } catch (\Exception $e) {
                return ['msg' => '操作失败' . $e->getMessage(), 'code' => 201];
            }
        }
    }

    public static function goRemove()
    {
        $data = Request::post();
        $model = ShortLink::find($data['id']);
        if ($model->isEmpty()) return ['msg' => '数据不存在', 'code' => 201];
        try {
            $model->delete();
        } catch (\Exception $e) {
            return ['msg' => '操作失败' . $e->getMessage(), 'code' => 201];
        }
    }
}